import {
	log
} from 'util'
import * as index from '@/api/index.js'
import {
	baseURL
} from '@/common/config.js'
export default {
	data() {
		return {
			form: {
				username: '',
				mobile: '',
				car_number: '',
				first: '',
				second: '',
				car_model: '',
				car_color: '',
				com_time: '',
				commit_time: '',
				worker: '',
				company: '',
				years: '',
				brand: '',
				model: '',
				info: "",
				con_part_ids: [],
				images: [],
				price: '',
				ps: ''
			},
			array: [{
					date: '无',
					value: 0
				},
				{
					date: '一年',
					value: 1
				},
				{
					date: '两年',
					value: 2
				},
				{
					date: '三年',
					value: 3
				},
				{
					date: '四年',
					value: 4
				},
				{
					date: '五年',
					value: 5
				}
			],
			index: 0,
			partList: [],
			partName: []
		}
	},

	onShow() {

	},
	onLoad(option) {
		this.getCarPartList()
		let that = this
		uni.$on('car', function(data) {
			let car = JSON.parse(data.msg)
			that.form.first = car.first
			that.form.second = car.second
			that.form.car_model = car.car_model
		})
	},

	methods: {
		chooseCar() {
			this.$tools.navigate('/pages/home/car/index')
		},
		bindPickerChange(e) {
			console.log(this.array[e.detail.value]);
			this.index = e.detail.value
			this.form.years = this.array[e.detail.value].value
		},
		//局部部件
		getCarPartList() {
			index.getCarPartList({
				con_id: 4
			}).then(res => {
				this.partList = res.data.con_part_list
			})
		},
		//上传视频
		chooseImage() {
			let that = this
			let count = 9 - that.form.images.length
			uni.chooseMedia({
				count: count,
				mediaType: ['image'],
				sourceType: ['album', 'camera'],
				camera: 'back',
				success(res) {
					// that.images.push(...res.tempFiles)
					res.tempFiles.forEach(item => {
						console.log(item);
						uni.uploadFile({
							url: baseURL + 'common/upload', //仅为示例，非真实的接口地址
							filePath: item.tempFilePath,
							name: 'file',
							formData: {
								'user': 'test',
								'Content-type': 'application/json',
								'token': uni.getStorageSync('token'),
							},
							success(images) {
								let url = JSON.parse(images.data).data.fullurl
								console.log(url);
								that.form.images.push(url)
							}
						})
					})

					console.log(res.tempFiles)
				}
			})


		},
		partClick(item, index) {
			if (this.form.con_part_ids.indexOf(item.id) < 0) {
				this.form.con_part_ids.push(item.id)
				this.partName.push(item.name)
			} else {
				this.removeByValue(this.form.con_part_ids, item.id)
				this.removeByValue(this.partName, item.name)
			}
		},
		removeByValue(arr, val) {
			for (var i = 0; i < arr.length; i++) {
				if (arr[i] == val) {
					arr.splice(i, 1);
					break;
				}
			}
		},
		submit() {
			let that = this
			uni.showModal({
				title: '提示',
				content: '是否确认提交质保信息',
				success: function(res) {
					if (res.confirm) {
						let form = JSON.parse(JSON.stringify(that.form))
						form.images = form.images.join()
						form.con_part_ids = form.con_part_ids.join()
						form.com_time = form.com_time / 1000
						form.commit_time = form.commit_time / 1000
						index.addWarranty(form).then(res => {
							that.$tools.toast('提交成功')
							setTimeout(()=>{
								that.$tools.back(1)
							},2000)
						})
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});

		}
	}
}